package com.jf.test.mvvm.base

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModel

/**
 * @Class: BaseFragVM
 * @Description:
 * @author: github.com/jackyflame
 * @Date: 2021/3/10
 */
abstract class BaseFragVM<out F : BaseDBFragment<*,*>, P:BasePresenter> : ViewModel() {

    var mPresenter: P? = null

    var mFragment: BaseDBFragment<*,*>? = null
        set(value) {
            field = value
            if(value != null){
                mPresenter = initPresenter(value);
                initData();
            }
        }

    abstract fun initPresenter(owner: LifecycleOwner): P?

    protected abstract fun initData()

    override fun onCleared() {
        super.onCleared()
        mFragment = null
        mPresenter = null
    }

    fun getFragment():F{
        return mFragment as F;
    }
}